package tree;

import stack.LinkedListStack;

import java.util.LinkedList;
import java.util.Queue;
import java.util.function.Consumer;

public class BST<E extends Comparable<E>> {

    private class Node {
        private E e;
        Node left, right;

        public Node(E e) {
            this.e = e;
        }
    }

    private Node root;
    private int size;

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public void add(E e) {
        root = add(root, e);
    }

    private Node add(Node node, E e) {
        if (node == null) {
            size++;
            return new Node(e);
        }

        if (e.compareTo(node.e) < 0) {
            node.left = add(node.left, e);
        } else {
            node.right = add(node.right, e);
        }

        return node;
    }

    public boolean contains(E e) {
        return contains(root, e);
    }

    private boolean contains(Node node, E e) {
        if (node == null) {
            return false;
        }
        if (e.compareTo(node.e) == 0) {
            return true;
        } else if (e.compareTo(node.e) < 0) {
            return contains(node.left, e);
        } else {
            return contains(node.right, e);
        }
    }

    public void preOrder(Consumer<E> consumer) {
        preOrder(root, consumer);
    }

    private void preOrder(Node node, Consumer<E> consumer) {
        if (node == null) return;
        consumer.accept(node.e);

        preOrder(node.left, consumer);
        preOrder(node.right, consumer);
    }

    public void inOrder(Consumer<E> consumer) {
        inOrder(root, consumer);
    }

    private void inOrder(Node node, Consumer<E> consumer) {
        if (node == null) return;

        inOrder(node.left, consumer);
        consumer.accept(node.e);
        inOrder(node.right, consumer);
    }

    public void postOrder(Consumer<E> consumer) {
        postOrder(root, consumer);
    }

    private void postOrder(Node node, Consumer<E> consumer) {
        if (node == null) return;

        postOrder(node.left, consumer);
        postOrder(node.right, consumer);
        consumer.accept(node.e);
    }

    public void preOrderByStack() {
        LinkedListStack<Node> stack = new LinkedListStack<>();
        Node node = root;
        stack.push(root);
        for (int i = 0; i < size; i++) {
            node = stack.pop();
            System.out.println(node.e);
            if (node.right != null) {
                stack.push(node.right);
            }
            if (node.left != null) {
                stack.push(node.left);
            }
        }
    }

    public void levelOrder() {
        Queue<Node> q = new LinkedList<>();
        q.add(root);
        while (!q.isEmpty()) {
            Node cur = q.remove();
            System.out.println(cur.e);

            if (cur.left != null) {
                q.add(cur.left);
            }
            if (cur.right != null) {
                q.add(cur.right);
            }
        }
    }

    public E minimum() {
        if (size == 0) {
            throw new IllegalArgumentException();
        }
        return minimum(root).e;
    }

    private Node minimum(Node node) {
        if (node.left == null) {
            return node;
        }
        return minimum(node.left);
    }

    public E maximum() {
        if (size == 0) {
            throw new IllegalArgumentException();
        }
        return maximum(root).e;
    }

    private Node maximum(Node node) {
        if (node.right == null) {
            return node;
        }
        return maximum(node.right);
    }

    public E removeMin() {
        E ret = minimum();
        root = removeMin(root);
        return ret;
    }

    private Node removeMin(Node node) {
        if (node.left == null) {
            Node rn = node.right;
            node.right = null;
            size--;
            return rn;
        }
        node.left = removeMin(node.left);
        return node;
    }

    public E removeMax() {
        root = removeMax(root);
        return maximum();
    }

    private Node removeMax(Node node) {
        if (node.right == null) {
            Node ln = node.left;
            node.left = null;
            size--;
            return ln;
        }
        node.right = removeMax(node.right);
        return node;
    }

    public void remove(E e) {
        root = remove(root, e);
    }

    private Node remove(Node node, E e) {
        if (node == null) {
            return null;
        }
        if (e.compareTo(node.e) < 0) {
            node.left = remove(node.left, e);
            return node;
        }
        else if (e.compareTo(node.e) > 0) {
            node.right = remove(node.right, e);
            return node;
        }
        else {
            if (node.left == null) {
                Node rightNode = node.right;
                node.right = null;
                size--;
                return rightNode;
            }
            if (node.right == null) {
                Node leftNode = node.left;
                node.left = null;
                size--;
                return leftNode;
            }
            // 找到node的后继
            Node successor = minimum(node.right);
            successor.right = removeMin(node.right);
            successor.left = node.left;
            node.right = node.left = null;
            return successor;
        }
    }

    public static void main(String[] args) {
        BST<Integer> bst = new BST<>();
        int[] nums = {5, 3, 6, 8, 4, 2, 7, 9};
        for (int num : nums) {
            bst.add(num);
        }
//        System.out.println(bst.contains(9));
//        bst.preOrder(e -> System.out.println(e));
//        System.out.println("========");

//        bst.inOrder(e -> System.out.println(e));
//        System.out.println("========");
//
//        bst.postOrder(e -> System.out.println(e));
//        System.out.println("========");

//        bst.preOrderByStack();
//        System.out.println("========");
//
        bst.levelOrder();
        System.out.println("========");

        bst.remove(8);
        bst.levelOrder();
    }
}
